from tools.aes_enc import count_encrypted_data,read_encrypted_data,decrypt_message_dict

def modify_user_nums(file_path, new_value):
    # 读取文件内容
    with open(file_path, 'r') as file:
        lines = file.readlines()

    # 修改内容
    new_lines = []
    for line in lines:
        if line.strip().startswith('user_nums'):
            # 替换等号后的值
            new_line = f'user_nums = {new_value}\n'
            new_lines.append(new_line)
        else:
            new_lines.append(line)

    # 将修改后的内容写回文件
    with open(file_path, 'w') as file:
        file.writelines(new_lines)

# 修改文件中的 user_nums 值
# file_path = 'C:\\Users\\29741\\Desktop\\test\\tools\\file_config.py'
# new_value = 5
# modify_user_nums(file_path, new_value)

# file_path = 'file_config.py'
# new_value = 5
# modify_user_nums(file_path, new_value)



def save_decrypted_data(file_num):
    output_file = f'file/user{file_num}/code{file_num}.py'
    # 获取加密数据条目数
    num_entries = count_encrypted_data(f'file/user{file_num}/code{file_num}.txt')
    print("Number of encrypted data entries:", num_entries)

    # 打开输出文件用于写入
    with open(output_file, 'w', encoding='utf-8') as output:
        for i in range(num_entries):
            # 读取加密数据从文件
            salt_ciphertext, encrypt_type, pri_key_or_psd = read_encrypted_data(f'file/user{file_num}/code{file_num}.txt', i)

            # 解密数据
            decrypted_message = decrypt_message_dict(salt_ciphertext, pri_key_or_psd, encrypt_type)
            print(f"Decrypted message {i + 1}:", decrypted_message)

            # 保存解密后的数据
            output.write(f"{decrypted_message}\n")


def modify_last_user(file_path, new_value):
    # 读取文件内容
    with open(file_path, 'r') as file:
        lines = file.readlines()

    # 修改内容
    new_lines = []
    for line in lines:
        if line.strip().startswith('last_user'):
            # 替换等号后的值
            new_line = f'last_user = {new_value}\n'
            new_lines.append(new_line)
        else:
            new_lines.append(line)

    # 将修改后的内容写回文件
    with open(file_path, 'w') as file:
        file.writelines(new_lines)

# file_path = 'file_config.py'
# new_value = 5
# modify_last_user(file_path, new_value)